<?php
    /**
     * First Page in the
     * installation procedure
     * followed by
     *  server setup: setupServer.php
     *  admin creation: setupAdmin.php
     *  and some other installation steps.
     */

    $pathAdjust = "../";
    include("{$pathAdjust}initIncludePaths.inc");   //paths to the other include scripts need to be initiated

    $error = "";
    /////////////////////////////
    ///// PHP Version Check /////
    /////////////////////////////
    $phpVersion = phpversion();
    $phptmp = str_replace(".", "",$phpVersion);
    if($phptmp >= "439") {
      $color = "green";
      $php = "true";
    } else {
      $color = "red";
      $error = "- Minimum: 4.3.9";
    }
    echo "<div style=\"width: 400px; padding: 4px; border-style: solid; border-width:4px; border-color: $color; text-align:justifiy;\">\n";
    echo "Your PHP-Version: $phpVersion  $error";
    echo "</div><br>";

    ///////////////////////////////
    ///// MYSQL Version Check /////
    ///////////////////////////////
    $mysqlVersion = mysql_get_client_info();
    $mysqltmp = str_replace(".", "",$mysqlVersion);
    if($mysqltmp >= "4120") {
        $color = "green";
        $mysql = "true";
    } else {
        $color = "red";
        $error = "- Minimum: 4.1.20";
    }
    echo "<div style=\"width: 400px; padding: 4px; border-style: solid; border-width:4px; border-color: $color; text-align:justifiy;\">\n";
    echo "Your MySQL-Version: $mysqlVersion  $error";
    echo "</div><br>";

    //////////////////////////
    ///// Download Check /////
    //////////////////////////
    include("environment.inc");
    $env = new Environment();

    $met = $env->convertFromIniValue(ini_get('max_execution_time'),$type);
    $nmet = $met+1;
    ini_set('max_execution_time', $nmet.$type);
    $smet = ini_get('max_execution_time');
    if($nmet == $smet) {
        echo "<div style=\"width: 400px; padding: 4px; border-style: solid; border-width:4px; border-color: green; text-align:justifiy;\">\n";
        echo"The max execution time for scripts can be automatically adjusted.<br>";
        echo"It is currently $met s<br>";
        echo "</div><br>";
            ini_set('max_execution_time', $met);
    } else {
        echo "<div style=\"width: 400px; padding: 4px; border-style: solid; border-width:4px; border-color: red; text-align:justifiy;\">\n";
        echo"The max execution time cannot be automatically adjusted.<br>";
        echo"It is currently $met s<br>";
        echo "</div><br>";
    }

    ////////////////////////////////////////////////////////////////
    /////			Max Upload
    ////////////////////////////////////////////////////////////////
    $met = $env->convertFromIniValue(ini_get('upload_max_filesize'),$type);
    $oval = $env->convertToIniValue($met,$type);
    $nmet = $met+1024*1024;
    $ival = $env->convertToIniValue($nmet,$type);
    ini_set('upload_max_filesize', $ival);
    $test = $env->convertFromIniValue(ini_get('upload_max_filesize'),$type);
    $nval = $env->convertToIniValue($test,$type);
    if($ival == $nval) {
            echo "<div style=\"width: 400px; padding: 4px; border-style: solid; border-width:4px; border-color: green; text-align:justifiy;\">\n";
            echo"The max upload property restricts the file size that can be uploaded to the server.<br>";
            echo"This affects your ability to upload sequences and measurements as well as images.<br>";
            echo"For sequences or data you may split one large into multiple smaller files in the worst case.<br><br>";
            echo "The max upload size for scripts can be automatically adjusted.<br>";
            echo"It is currently $oval<br>";
            ini_set('upload_max_filesize', $oval);
    } else {
        echo "<div style=\"width: 400px; padding: 4px; border-style: solid; border-width:4px; border-color: red; text-align:justifiy;\">\n";
        echo"The max upload property restricts the file size that can be uploaded to the server.<br>";
        echo"This affects your ability to upload sequences and measurements as well as images.<br>";
        echo"For sequences or data you may split one large into multiple smaller files in the worst case.<br><br>";
        echo"<font color='red'>The max upload file size cannot be automatically adjusted.<br>";
        echo"It is currently $oval</font><br><br>";
        echo"In order to increase this value, you need to contact your ISP.<br>";
    }
    echo "</div><br>";

    ////////////////////////////////////////////////////////////////
    /////			Max Post
    ////////////////////////////////////////////////////////////////
    $met = $env->convertFromIniValue(ini_get('post_max_size'),$type);
    $oval = $env->convertToIniValue($met,$type);
    $nmet = $met+1024*1024;
    $ival = $env->convertToIniValue($nmet,$type);
    ini_set('post_max_size', $ival);
    $test = $env->convertFromIniValue(ini_get('post_max_size'),$type);
    $nval = $env->convertToIniValue($test,$type);
    //echo"O: $oval, I: $ival, N: $nval<br>";
    if($ival == $nval) {
        echo "<div style=\"width: 400px; padding: 4px; border-style: solid; border-width:4px; border-color: green; text-align:justifiy;\">\n";
        echo "This option affects the max size of post data allowed. This setting also affects file upload. To upload large files, this value must be larger than upload_max_filesize.<br><br>";
        echo "The max upload size for scripts can be automatically adjusted.<br>";
        echo"It is currently $oval<br>";
        ini_set('post_max_size', $oval);
    } else {
        echo "<div style=\"width: 400px; padding: 4px; border-style: solid; border-width:4px; border-color: red; text-align:justifiy;\">\n";
        echo "This option affects the max size of post data allowed. This setting also affects file upload. To upload large files, this value must be larger than upload_max_filesize.<br><br>";
        echo"<font color='red'>The max upload file size cannot be automatically adjusted.<br>";
        echo"It is currently $oval</font><br><br>";
        echo"In order to increase this value, you need to contact your ISP.<br>";
    }
    echo "</div><br>";

    ////////////////////////////////////////////////////////////////
    /////			Max memory
    ////////////////////////////////////////////////////////////////
    $met = $env->convertFromIniValue(ini_get('memory_limit'),$type);
    $oval = $env->convertToIniValue($met,$type);
    $nmet = $met+1024*1024;
    $ival = $env->convertToIniValue($nmet,$type);
    ini_set('memory_limit', $ival);
    $test = $env->convertFromIniValue(ini_get('memory_limit'),$type);
    $nval = $env->convertToIniValue($test,$type);
    //echo"OO: $met, O: $oval, I: $ival, N: $nval<br>";
    if($ival == $nval) {
        echo "<div style=\"width: 400px; padding: 4px; border-style: solid; border-width:4px; border-color: green; text-align:justifiy;\">\n";
        echo "This sets the maximum amount of memory in bytes that a script is allowed to allocate. This helps prevent poorly written scripts for eating up all available memory on a server. It may impair the upload of new data to the database if it is too low (less than 100 MB).<br><br>";
        echo "The max memory size for scripts can be automatically adjusted.<br>";
        echo"It is currently $oval<br>";
        ini_set('memory_limit', $oval);
    } else {
        echo "<div style=\"width: 400px; padding: 4px; border-style: solid; border-width:4px; border-color: red; text-align:justifiy;\">\n";
        echo "This sets the maximum amount of memory in bytes that a script is allowed to allocate. This helps prevent poorly written scripts for eating up all available memory on a server. It may impair the upload of new data to the database if it is too low (less than 100 MB).<br><br>";
        echo"<font color='red'>The max memory size cannot be automatically adjusted.<br>";
        echo"It is currently $oval</font><br><br>";
        echo"In order to increase this value, you need to contact your ISP.<br>";
    }
    echo "</div><br>";


    if($env->canUpload()) {
        $maxFileUpSize = $env->determineMaxUpload();
        $upload = "true";
        echo "<div style=\"width: 400px; padding: 4px; border-style: solid; border-width:4px; border-color: green; text-align:justifiy;\">\n";
        echo"You are allowed to upload files!";
        echo "</div><br>";
        echo "<div style=\"width: 400px; padding: 4px; border-style: solid; border-width:4px; border-color: green; text-align:justifiy;\">\n";
        echo "Maximum file size: ",
        number_format($maxFileUpSize/1024/1024, 0, ",", "."),
        " MB (",
        $maxFileUpSize,
        " Bytes)<br>";
        echo"Files you can upload such as sequences may not exeed this limit.";
        echo "</div><br>";
    } else {
        echo "<div style=\"width: 400px; padding: 4px; border-style: solid; border-width:4px; border-color: red; text-align:justifiy;\">\n";
        echo"You are not allowed to upload files!";
        echo "</div><br>";
    }
?>